package com.bookstore.controller;

import com.bookstore.domain.Error;
import com.bookstore.domain.ShoppingCart;
import com.bookstore.domain.User;
import com.bookstore.service.ShoppingCartService;
import com.bookstore.utils.JsonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService service;

    @RequestMapping("/getShoppingCarts.do")
    @ResponseBody
    public String getShoppingCartByUid(Integer start, Integer uid) {
        //处理数据
        if (start == null) {
            start = 1;
        }
        //从第一页开始，一页5行
        PageHelper.startPage(start, 5);
        List<ShoppingCart> adds = service.findAllByUid(uid);
        PageInfo<ShoppingCart> pages = new PageInfo<ShoppingCart>(adds);
        return JsonUtils.writeValueAsString(pages);
    }

    //添加购物车
    @RequestMapping("/addCart.do")
    @ResponseBody
    public String addCart(Integer bid, HttpServletRequest req) {
        //获得登录的用户信息
        User user = (User) req.getSession().getAttribute("loginUser");
        Error e = new Error();
        if(user == null){
            e.setErrorInfo("请先登录。。");
            e.setErrorType(1);
            return JsonUtils.writeValueAsString(e);
        }
        //获取数量
        String count_str = req.getParameter("count");
        Integer count = 1;
        int result = 0;
        if (count_str != null && !count_str.equals("")) {
            count = Integer.parseInt(count_str);
        }
        //查询购物车  查看是否有这个商品 有的话 数量等于原来数量+1
        ShoppingCart old = service.findAllByUidAndBid(user.getUid(), bid);
        if(old != null){
            //执行修改操作
            result = service.upCount(old.getUid().getUid(),old.getBid().getBid(),old.getCount()+count);
        }else {
            //执行添加
            result = service.addCart(bid, user.getUid(), count);
        }
        if (result == 0){
            e.setErrorType(2);
            e.setErrorInfo("加入购物车失败，请稍后再试。。。");
        }else if(result == 1){
            e.setErrorType(0);
            e.setErrorInfo("加购成功！");
        }
        return JsonUtils.writeValueAsString(e);
    }
    //删除购物车数据
    @RequestMapping("/delCart.do")
    @ResponseBody
    public String delCart(Integer bid, HttpServletRequest req) {
        //获得登录的用户信息
        User user = (User) req.getSession().getAttribute("loginUser");
        Error e = new Error();
        if(user == null){
            e.setErrorInfo("请重新登录。。");
            e.setErrorType(1);
        }
        int result = 0;
        result = service.delCart(bid,user.getUid());
        if (result == 0){
            e.setErrorType(2);
            e.setErrorInfo("网络开小差咯。。。");
        }else if(result == 1){
            e.setErrorType(0);
            e.setErrorInfo("删除成功！");
        }
        return JsonUtils.writeValueAsString(e);
    }
}
